package pong.ui.window;

import java.awt.Color;
import java.awt.Graphics;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import pong.business.controller.ControllerFacade;
import pong.business.controller.GameController;
import pong.business.events.PongElementListener;
import pong.ui.element.PongBall;
import pong.ui.element.PongBat;

@SuppressWarnings("serial")
public class GamePanel extends JPanel implements PongElementListener {

	private static GamePanel instance;
	
	private ControllerFacade controller;

	private PongBall ball = new PongBall();
	
	private PongBat leftBat = new PongBat();
	
	private PongBat rightBat = new PongBat();
	
	private GamePanel() {
		setBackground(Color.BLACK);
		setBorder(BorderFactory.createRaisedBevelBorder());
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		ball.draw(g);
		leftBat.draw(g);
		rightBat.draw(g);
	}
	
	public static GamePanel getInstance() {
		if(instance == null) {
			instance = new GamePanel();
		}
		return instance;
	}
	
	public ControllerFacade getController() throws FileNotFoundException, IOException {
		if(controller == null) {
			controller = (ControllerFacade) new GameController(this);
		}
		return controller;
	}

	public void ballPositionUpdated(int x, int y) {
		ball.getPosition().move(x, y);
		repaint();
	}

	public void leftPlayerPositionUpdated(int x, int y) {
		leftBat.getPosition().move(x, y);
		repaint();
	}

	public void rightPlayerPositionUpdated(int x, int y) {
		rightBat.getPosition().move(x, y);
		repaint();
	}
}
